/*
 * JTalkerFrontCommand.java
 * This Form lets user input a command and send it to the server (or another user)
 * Created on 10. März 2005, 19:19
 */

/**
 *
 * @author  christoph
 */

package info.chrm.jtalker;

public class JTalkerFrontCommand extends javax.swing.JFrame implements JTalkerClientCommandInterface {
    private JTalkerClientFront parent = null;
    private String lastResponse = null;
    
    /** Creates new form JTalkerFrontCommand */
    public JTalkerFrontCommand(JTalkerClientFront _parent) {
        this.parent = _parent;
        initComponents();
	this.setLocationRelativeTo(_parent);
        this.parent.theClient.registerForCommandResponse(this);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     */
    private void initComponents() {
        jPanelTop = new javax.swing.JPanel();
        jComboBox_COMMANDS = new javax.swing.JComboBox();
        jButton_SEND = new javax.swing.JButton();
        jPanelBottom = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        jCheckBox_FORMAT = new javax.swing.JCheckBox();
        jButton_CLOSE = new javax.swing.JButton();
        jPanelCenter = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextPaneResponse = new javax.swing.JTextPane();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        jComboBox_COMMANDS.setEditable(true);
        jComboBox_COMMANDS.setModel(
	      new javax.swing.DefaultComboBoxModel(
		 new String[] { "No methods available. Connect first!" }));
        jComboBox_COMMANDS.setPreferredSize(new java.awt.Dimension(350, 24));
        jPanelTop.add(jComboBox_COMMANDS);

        jButton_SEND.setText("Send");
        jButton_SEND.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_SENDActionPerformed(evt);
            }
        });

        jPanelTop.add(jButton_SEND);
        jButton_SEND.getAccessibleContext().setAccessibleName("jButton_SEND");

        getContentPane().add(jPanelTop, java.awt.BorderLayout.NORTH);

        jPanelBottom.setLayout(new java.awt.BorderLayout());

        jPanel1.setLayout(new java.awt.BorderLayout());

        jCheckBox_FORMAT.setText("Format output");
        jCheckBox_FORMAT.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBox_FORMATActionPerformed(evt);
            }
        });

        jPanel1.add(jCheckBox_FORMAT, java.awt.BorderLayout.CENTER);

        jPanelBottom.add(jPanel1, java.awt.BorderLayout.CENTER);

        jButton_CLOSE.setText("Close");
        jButton_CLOSE.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton_CLOSEActionPerformed(evt);
            }
        });

        jPanelBottom.add(jButton_CLOSE, java.awt.BorderLayout.EAST);

        getContentPane().add(jPanelBottom, java.awt.BorderLayout.SOUTH);

        jPanelCenter.setLayout(new java.awt.BorderLayout());

        jPanelCenter.setPreferredSize(new java.awt.Dimension(10, 240));
        jScrollPane1.setMinimumSize(new java.awt.Dimension(220, 220));
        jScrollPane1.setViewportView(jTextPaneResponse);

        jPanelCenter.add(jScrollPane1, java.awt.BorderLayout.CENTER);

        getContentPane().add(jPanelCenter, java.awt.BorderLayout.CENTER);

        pack();
    }

    private void jCheckBox_FORMATActionPerformed(java.awt.event.ActionEvent evt) {	
       this.showResponse();
    }

    /** Send clicked */
    private void jButton_SENDActionPerformed(java.awt.event.ActionEvent evt) {
        if(!this.parent.theClient.isConnected)
	   return;
	
        this.parent.theClient.sendMessage((String)this.jComboBox_COMMANDS.getSelectedItem(), null, "command", null, "usercommand01");
    }
    
    /** A command answer will be send to us from the server using this method 
     *  @return true, if the _id belongs to our command */
    public boolean setCommandAnswer(String _id, String _from, String _content) {
        if(_id.equals("usercommand01")) {
            this.lastResponse = _content;

            if(this.lastResponse != null)
                this.showResponse();

            return true;
        } else if(_id.equals("getmethods01")) {
            // TODO: check this!
            try { 
                if(_content == null)
                    return false;

                String [] allMethods = _content.split(";;");
                jComboBox_COMMANDS.setModel(
                  new javax.swing.DefaultComboBoxModel(allMethods));
                this.methodsLoaded = true;
            } catch (Exception _e) {
                System.err.println("*** Can't get server methods: " + _e.toString());
            }

            return true;
        }

        return false;
    }

    /** Shows the command response */
    private void showResponse() {
       if(this.lastResponse == null)
	  return;
       
       if(this.jCheckBox_FORMAT.isSelected())
	 this.jTextPaneResponse.setText(this.lastResponse.replaceAll(";;", "\n"));
        else
	 this.jTextPaneResponse.setText(this.lastResponse);
    }

    private void jButton_CLOSEActionPerformed(java.awt.event.ActionEvent evt) {
        this.dispose();
    }

    // Overwrite setVisible for updating server methods
    public void setVisible(boolean _visible) {
        if(!this.methodsLoaded &&this.parent.theClient.sendMessage("std::getMethods()", null, "command", null, "getmethods01"));
        super.setVisible(_visible);
    }
    
    
    private javax.swing.JButton jButton_CLOSE;
    private javax.swing.JButton jButton_SEND;
    private javax.swing.JCheckBox jCheckBox_FORMAT;
    private javax.swing.JComboBox jComboBox_COMMANDS;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanelBottom;
    private javax.swing.JPanel jPanelCenter;
    private javax.swing.JPanel jPanelTop;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextPane jTextPaneResponse;
    private boolean methodsLoaded = false;
    
}
